package com.appsl.manager;

import com.appsl.objects.Lugar;
import com.appsl.objects.Option;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

public class ListManager 
{
	public static Spinner setList(Activity act, Spinner cbxTarget, String[] items)
	{
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_dropdown_item, 
																items);
		cbxTarget.setAdapter(adapter);
		return cbxTarget;
	}
	
	public static ListView setList(Activity act, ListView lvTarget, List<?> lstLocation)
	{
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		
		for (Object obj : lstLocation)
		{
			Map<String, String> datum = new HashMap<String, String>(2);
			
			if (obj.getClass() == Lugar.class)
			{			
				Lugar objLocation = (Lugar)obj;
				datum.put("item", objLocation.getName());
				datum.put("subitem", String.format("%f|%f", objLocation.getX(), objLocation.getY()));
			}		
			if (obj.getClass() == Option.class)
			{			
				Option objOption = (Option)obj;
				datum.put("item", objOption.getName());
			}			
			data.add(datum);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(act, data, android.R.layout.simple_list_item_2,
												  new String[] { "item", "subitem" },
												  new int[] { android.R.id.text1, android.R.id.text2 });									
		lvTarget.setAdapter(adapter);
		return lvTarget;
	}
	
}
